using JGSY.CMS.LowCode.Platform.Domain.Enums;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 工作流实例实体
    /// 表示工作流的具体执行实例，记录工作流执行过程中的状态和数据
    /// 对应数据库表：workflow_instances
    /// </summary>
    [Table("workflow_instances")]
    public class WorkflowInstance : ITenantEntity
    {
        /// <summary>
        /// 工作流实例标识符
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// 用于多租户数据隔离
        /// </summary>
        [Required]
        [StringLength(36)]
        [Column("tenant_id")]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 工作流标识符
        /// 所属工作流定义的ID
        /// </summary>
        [Required]
        public long WorkflowId { get; set; }

        /// <summary>
        /// 实例名称
        /// 工作流实例的显示名称，最大长度100字符
        /// </summary>
        [Required]
        [StringLength(100)]
        public string InstanceName { get; set; } = string.Empty;

        [Required]
        public WorkflowInstanceStatus Status { get; set; } = WorkflowInstanceStatus.Pending;

    [StringLength(100)]
    public string? CurrentNode { get; set; }

        [Required]
        public long InitiatorId { get; set; }

        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        public DateTime UpdatedAt { get; set; }

        public DateTime? CompletedAt { get; set; }

    [Column(TypeName = "text")]
    public string? ContextData { get; set; }

    [StringLength(500)]
    public string? Description { get; set; }

        public int Priority { get; set; } = 0;

        // Navigation properties
        [ForeignKey(nameof(WorkflowId))]
    public virtual Workflow Workflow { get; set; } = null!;

        [ForeignKey(nameof(InitiatorId))]
    public virtual User Initiator { get; set; } = null!;

        public virtual ICollection<WorkflowTask> Tasks { get; set; } = new List<WorkflowTask>();
    }
}
